<template>
  <a-spin :spinning="pageLoading">
    <a-form-model class="detail-form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="职位名称">
        <a-input allowClear v-model="form.position"/>
      </a-form-model-item>
      <a-form-model-item label="开放联系数">
        <a-input allowClear v-model="form.openNum"/>
      </a-form-model-item>
      <a-form-model-item label="公司名称">
        <a-input allowClear v-model="form.companyName"/>
      </a-form-model-item>
      <a-form-model-item label="需求人数">
        <a-input allowClear v-model="form.num"/>
      </a-form-model-item>
      <a-form-model-item label="预算">
        <a-input allowClear v-model="form.budget"/>
      </a-form-model-item>
      <a-form-model-item label="简介">
        <a-input allowClear v-model="form.desc"/>
      </a-form-model-item>
      <a-form-model-item label="需求类型">
        <a-cascader :options="Config.serviceOptions" placeholder="请选择" v-model="form.service"/>
      </a-form-model-item>
      <a-form-model-item label="用人城市">
        <a-cascader :options="Config.cityOptions" placeholder="请选择" v-model="form.area"/>
      </a-form-model-item>
      <a-form-model-item label="角色">
        <a-select allowClear placeholder="请选择" v-model="form.role">
          <a-select-option :value="v.id" v-for="(v, i) in Config.OrderRole.filter(i => {return i.id > 0})" :key="i">{{ v.title }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="发布者">
        <a-select allowClear placeholder="请选择" v-model="form.userId">
          <a-select-option :value="v.id" v-for="(v, i) in userList" :key="i">{{ v.name }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 14, offset: 5 }">
        <a-button type="primary" @click="onSubmit"> 提交</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-spin>
</template>
<script>

import config from "../../tools/config";

function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

export default {
  data() {
    return {
      // 公用配置
      Config: {},
      userList: [],
      pageLoading: false,
      uploadLoading: false,
      labelCol: {span: 5},
      wrapperCol: {span: 14},
      form: {
        workingImage: '',
        contractState: 1,
        email: null,
        enable: true,
        followState: 0,
        image: "",
        mobile: "",
        mobileState: 1,
        name: "",
        nickname: "",
        password: "",
        subscribeState: 0,
        supplier: null,
        supplierId: null,
        type: '',
        usState: 1
      },
    };
  },
  created() {
    this.Config = config
    this.getUserList()
  },
  methods: {
    getUserList() {
      this.Http.get("/admin/user/getAllList", {type: 2}).then(res => {
        if (res.code == 1) {
          this.userList = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 提交更新
    onSubmit() {
      this.form.service = this.form.service[this.form.service.length - 1]
      this.form.area = this.form.area[this.form.area.length - 1]
      this.Http.post("/admin/order/create", this.form).then(res => {
        if (res.code == 1) {
          this.$message.success(res.msg)
          setTimeout(() => {
            location.reload()
          }, 1500)
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 上传
    handleChange(info) {
      console.log(info.event)
      if (info.file.status === 'uploading') {
        this.uploadLoading = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        getBase64(info.file.originFileObj, imageUrl => {
          this.form.workingImage = imageUrl;
          this.uploadLoading = false;
        });
      }
    },

    // 上传前检测格式和大小
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      if (!isJpgOrPng) {
        this.$message.error('You can only upload JPG file!');
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('Image must smaller than 2MB!');
      }
      return isJpgOrPng && isLt2M;
    },
  },
};
</script>
